<?php
//开启会话
session_start();
?>
<?php
include_once 'admin/connect.php';

$sql = "select * from text where adminName = 'admin' ";
$result = mysqli_query($connect, $sql);
if (mysqli_num_rows($result)) {
    $text = mysqli_fetch_array($result);
} else {
    header("Location:admin/login.php");
    die("暂无权限");
}
$nub = "select count(id) as shu from leaving";
$res = mysqli_query($connect,$nub);
$leav = mysqli_fetch_array($res);
$shu = $leav['shu'];
$liuyan = "select * from leaving order by id desc";
$resliuyan = mysqli_query($connect,$liuyan);
$ip = $_SERVER['REMOTE_ADDR'];
?>
<!DOCTYPE html>
<html>
<head>
    <?php
    include_once 'head.php';
    ?>

    <meta charset="utf-8" />
    <title><?php echo $text['title']?> — <?php echo $text['card2']?></title>
    <link rel="stylesheet" href="css/leaving.css">
    <script src="font_leav/iconfont.js"></script>
</head>
<body>


<div class="central central-800 bg">
    <h3>已收到 <b><?php echo $leav['shu']?></b> 条祝福留言</h3>
    <div class="row">
        <div class="card col-lg-12 col-md-12 col-sm-12 col-sm-x-12">
            <?php
            while ($info = mysqli_fetch_array($resliuyan)) {
                ?>
            <div class="leavform">
                <img src="https://q1.qlogo.cn/g?b=qq&nk=<?php echo $info['QQ']?>&s=640" alt="">
                <div class="textinfo">
                    <span class="name"><?php echo $info['name']?></span>
                    <b></b>
                    <span class="name ipclass"><?php echo get_ip_city($info['ip']) ?></span>
                    <i class="time"><?php echo $info['time']?></i>
                    <div class="text"><?php echo $info['text']?></div>
                </div>
            </div>
            <?php
            }
            ?>
            <form action="admin/leavingPost.php" method ="post" onsubmit="return check()">
            <div class="inputbox">
                <svg class="icon sm22" aria-hidden="true">
                    <use xlink:href="#icon-QQ1"></use>
                </svg>
                <input type="text" name="qq" placeholder="QQ号码" class="rig">
                <svg class="icon sm22" aria-hidden="true">
                    <use xlink:href="#icon-xunzhang"></use>
                </svg>
                <input type="text" name="name" placeholder="昵称" class="let">
            </div>
                <svg class="icon sm22 bu2" aria-hidden="true">
                    <use xlink:href="#icon-shoucang"></use>
                </svg>
            <textarea name="text" id="wenben" rows="8" placeholder="请输入您的留言内容"></textarea>
            <div class="input-sub">
                <input name="ip" value="<?php echo $ip ?>" type="hidden">
                <input type="submit" value="提交" class="tijiao">
            </div>
            </form>
        </div>
    </div>
</div>



<!-- footer版权 -->
<div class="footer-warp">
    <div class="footer">
        <p><?php echo $text['icp']?></p>
        <p><?php echo $text['Copyright']?></p>
    </div>
</div>
<script>
    function check() {
        //获取name数组中的第0个索引 并且去掉空格
        let qq = document.getElementsByName('qq')[0].value.trim();
        let name = document.getElementsByName('name')[0].value.trim();
        // 判断adminName长度是否为0 如果为0则提示弹窗
        if (qq.length == 0) {
            alert("请填写QQ号码");
            return false;
        } else if (name.length == 0) {
            alert("请填写恁的昵称");
            return false;
        }
        // 判断QQ号码
        let qqlength = /^[0-9]{6,10}$/;
        if (!qqlength.test(qq)){
            alert("您的QQ号码格式错误 \n 请输入由6-10位的数字组成的QQ号码");
            return false;
        }
        if((qq == 123456) || (qq == 100000) ||(qq == 1234567)){
            alert("我想也许这并不是您的QQ号码...");
            return false;
        }
        let text = document.getElementsByName('text')[0].value.trim();
        if (text.length == 0){
            alert("请填写您要留言的内容");
            return false;
        }else if (text.length <= 2){
            alert("请填写两个字符以上的内容");
            return false;
        }
        let nonub = /^[0-9]+$/;
        let filter = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]");
        if(filter.test(text)){
            alert("您输入的内容含有非法字符")
            return false;
        }else if(nonub.test(text)){
            alert("内容为纯数字 已被拦截")
            return false;
        }

    }

</script>
<?php

function get_ip_city($ip){
    $ch = curl_init();
    $url = 'https://whois.pconline.com.cn/ipJson.jsp?ip=' . $ip;
    //用curl发送接收数据
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //请求为https
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $location = curl_exec($ch);
    curl_close($ch);
    //转码
    $location = mb_convert_encoding($location, 'utf-8', 'GB2312');
    //var_dump($location);
    //截取{}中的字符串
    $location = substr($location, strlen('({') + strpos($location, '({'), (strlen($location) - strpos($location, '})')) * (-1));
    //将截取的字符串$location中的‘，’替换成‘&’   将字符串中的‘：‘替换成‘=’
    $location = str_replace('"', "", str_replace(":", "=", str_replace(",", "&", $location)));
    //php内置函数，将处理成类似于url参数的格式的字符串  转换成数组
    parse_str($location, $ip_location);
    return $ip_location['pro'];
}
?>

</body>
</html>
